//$URL: http://dasding.googlecode.com/svn/trunk/data/dao/mongoDB/src/main/java/de/piratech/dasding/data/database/mongo/EventDataImpl.java $
//$Id: EventDataImpl.java 125 2013-04-07 11:42:35Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.database.mongo;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.DB;

import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.Event;
import de.piratech.dasding.data.Location;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.data.database.EventData;
import de.piratech.dasding.data.database.mongo.data.MongoEvent;
import de.piratech.dasding.data.database.mongo.data.MongoEventCategory;
import de.piratech.dasding.data.database.mongo.data.MongoLocation;
import de.piratech.dasding.data.database.mongo.data.MongoUser;

/**
 */
public class EventDataImpl extends AbstractMongoDB<MongoEvent, Event> implements
    EventData {

  Logger LOG = LoggerFactory.getLogger(EventDataImpl.class);

  protected EventDataImpl(DB pDB) {
    super(pDB, MongoEvent.class, COLLECTIONS.EVENT_COLLECTION);
  }

  @Override
  public Event createEvent(Event pEvent) throws DatabaseException {
    if (!pEvent.isValid()) {
      throw new DatabaseException("event is not valid");
    }
    checkObjectExists(COLLECTIONS.USER_COLLECTION.getName(),
        pEvent.getContact());

    checkObjectExists(COLLECTIONS.EVENTCATEGORY_COLLECTION.getName(),
        pEvent.getCategory());

    return getDBCollection().insert(new MongoEvent(pEvent)).getSavedObject()
        .getDataObject();
  }

  @Override
  public void deleteEvent(Event pEvent) throws DatabaseException {
    List<Action> action = new ActionDataImpl(db).findActionByEvent(pEvent
        .getId());
    if (action != null && action.size() > 0) {
      throw new DatabaseException("there are related actions");
    }
    getDBCollection().removeById(pEvent.getId());
  }

  @Override
  public void updateEvent(Event pEvent) {
    MongoEvent event = new MongoEvent(pEvent);
    getDBCollection().updateById(pEvent.getId(), event);
  }

  @Override
  public Event getEvent(String pId) throws DatabaseException {
    MongoEvent oneById = getDBCollection().findOneById(pId);
    if (oneById != null) {
      return oneById.getDataObject();
    } else {
      throw new DatabaseException("event does not exist");
    }
  }

  @Override
  public List<Event> getEvents() {
    List<MongoEvent> mongoEvents = getDBCollection().find().toArray();
    List<Event> events = new ArrayList<Event>();
    for (MongoEvent mongoEvent : mongoEvents) {
      events.add(mongoEvent.getDataObject());
    }
    return events;
  }

  @Override
  public List<Event> findByCategories(List<String> pCategory)
      throws DatabaseException {
    List<Event> events = new ArrayList<Event>();
    for (String categoryId : pCategory) {
      List<MongoEvent> array = findByObjectReference("categoryRef",
          COLLECTIONS.EVENTCATEGORY_COLLECTION.getName(), categoryId,
          MongoEventCategory.class);
      events.addAll(getDataObjectList(array));

    }
    return events;
  }

  public List<Event> findByCreator(User pUser) {
    List<MongoEvent> array = findByObjectReference("contactRef",
        COLLECTIONS.USER_COLLECTION.getName(), pUser.getId(), MongoUser.class);
    return getDataObjectList(array);
  }

  public List<Event> findByLocation(Location location) {
    List<MongoEvent> mongoEvents = findByObjectReference("locationRef",
        COLLECTIONS.LOCATION_COLLECTION.getName(), location.getId(),
        MongoLocation.class);
    List<Event> list = getDataObjectList(mongoEvents);
    return list;
  }
}
